using ProjectDanmaku.Entity;
using ProjectDanmaku.Entity.Manipulator;
using ProjectDanmaku.Util;
using UnityEngine;

namespace ProjectDanmaku.Enemy.Boss.Nightbug
{
    public partial class BossNightbugController
    {
        private class BossNightbugBulletLogicLType : EntityLogicOverride
        {
            private int deg;
            private SimpleMotionManipulator _simpleMotionManipulator = new ();
            private ShootableCollidePhysicsManipulator _shootablePhysicsManipulator = new ();
            private DeathZoneCheckManipulator _deathZoneCheckManipulator = new ();

            public BossNightbugBulletLogicLType(int deg)
            {
                _shootablePhysicsManipulator.SetLayerMask(BitUtility.EverythingExcept(1));
                this.deg = deg;
            }
            
            private float AnimCurve(float t)
            {
                return Mathf.Pow(t, 0.25f);
            }
            
            public override void EntityLogicTick(EntityData entity)
            {
                entity.tick += Time.deltaTime;

                if (entity.tick <= 1)
                {
                    var targetPos = new Vector2(
                        entity.floatReg0 * 0.4f + entity.floatReg2, entity.floatReg1 * 0.4f + entity.floatReg3);
                    var oriPos = new Vector2(entity.floatReg2, entity.floatReg3);

                    entity.Position = Vector2.Lerp(oriPos, targetPos, AnimCurve(entity.tick * 3));
                }
                else if (entity.intReg3 == 0)  // state reg
                {
                    entity.Velocity = MathHelper.RotateVec2(entity.Velocity, deg);
                    entity.RotateZ = MathHelper.Vec2ToRotateY(entity.Velocity);

                    entity.intReg3 = 1;
                }
                else if (entity.tick > 1)
                {
                    _simpleMotionManipulator.Do(entity);
                }
            }

            public override void EntityPhysicsTick(EntityData entity)
            {
                _shootablePhysicsManipulator.Do(entity);
            }

            public override void EntityLateTick(EntityData entity)
            {
                screenBoundCheckManipulator.Do(entity);
                _deathZoneCheckManipulator.Do(entity);
            }

            public override void EntityInit(EntityData entity)
            {
                entity.floatReg0 = entity.Velocity.normalized.x;
                entity.floatReg1 = entity.Velocity.normalized.y;
                entity.floatReg2 = entity.Position.x;
                entity.floatReg3 = entity.Position.y;
            }
        }
    }
}